<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="角色ID" prop="roleId">
        <el-input v-model="dataForm.roleId" placeholder="请输入角色ID"></el-input>
      </el-form-item>
      <el-form-item label="菜单ID" prop="menuId">
        <el-input v-model="dataForm.menuId" placeholder="请输入菜单ID"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="请输入创建时间"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="请输入更新时间"></el-input>
      </el-form-item>
      <el-form-item label="删除标记" prop="isDeleted">
        <el-select v-model="dataForm.isDeleted" placeholder="请选择删除标记">
          <el-option label="可用 (1)" value="1"></el-option>
          <el-option label="不可用 (0)" value="0"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        roleId: '',
        menuId: '',
        createTime: '',
        updateTime: '',
        isDeleted: ''
      },
      dataRule: {
        roleId: [
          { required: true, message: '角色ID不能为空', trigger: 'blur' }
        ],
        menuId: [
          { required: true, message: '菜单ID不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ],
        isDeleted: [
          { required: true, message: '删除标记不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/system/sysrolemenu/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.roleId = data.sysRoleMenu.roleId
              this.dataForm.menuId = data.sysRoleMenu.menuId
              this.dataForm.createTime = data.sysRoleMenu.createTime
              this.dataForm.updateTime = data.sysRoleMenu.updateTime
              this.dataForm.isDeleted = data.sysRoleMenu.isDeleted.toString() // 确保类型一致
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/system/sysrolemenu/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'roleId': this.dataForm.roleId,
              'menuId': this.dataForm.menuId,
              'createTime': this.dataForm.createTime,
              'updateTime': this.dataForm.updateTime,
              'isDeleted': this.dataForm.isDeleted
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          }).catch(e => {
            this.visible = false
            this.$emit('refreshDataList')
            console.log(e)
          })
        }
      })
    }
  }
}
</script>